﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Empires.RAL.Access;
using Empires.CRL.DataContract;
using Empires.RAL.ORM;
using Empires.RAL.ORM.CompanyData;

namespace Empires.RAL.Pump
{
    public class CompanyProductDataPump : ICompanyProductAccess
    {
        public List<DefaultDataPack> GetProductWithList(string copName, string type, string subject, int page, int size)
        {
            using (mysql_companyEntities ctx = new mysql_companyEntities())
            {
                var queryable = (from c in ctx.tb_product
                              where c.c_vc_cpname == copName
                              orderby c.c_i_id descending
                              select new
                              {
                                  c.c_i_type,
                                  c.c_i_subject,
                                  c.c_nvc_title,
                                  c.c_dt_pbdate,
                                  c.c_i_hit,
                                  c.c_i_id,
                                  c.c_nvc_author,
                                  c.c_nvc_content,
                                  c.c_nvc_img
                              })
                              .Skip(page * size)
                              .Take(size);
                if (!String.IsNullOrEmpty(type))
                {
                    queryable = queryable.Where(c => c.c_i_type == Int32.Parse(type));
                }
                if (!String.IsNullOrEmpty(subject))
                {
                    queryable = queryable.Where(c => c.c_i_subject == Int32.Parse(subject));
                }
                var result = queryable.ToList()
                              .Select(c => new DefaultDataPack()
                              {
                                  TITLE = c.c_nvc_title,
                                  ABSTRACT = CRL.Tools.StringPlus.GetStringWithoutHTMLLabel(c.c_nvc_content),
                                  AUTHOR = c.c_nvc_author,
                                  DATE = c.c_dt_pbdate.ToShortDateString(),
                                  HIT = c.c_i_hit,
                                  ID = c.c_i_id,
                                  URL = c.c_nvc_img
                              })
                              .Select(c =>
                              {
                                  c.ABSTRACT = c.ABSTRACT.Length > 130 ? c.ABSTRACT.Substring(0, 130) : c.ABSTRACT;
                                  return c;
                              })
                              .ToList();
                return result;
            }
        }

        public int GetProductItemCount(string copName, string type, string subject)
        {
            using (mysql_companyEntities ctx = new mysql_companyEntities())
            {
                var result = from c in ctx.tb_product
                             where c.c_vc_cpname == copName
                             select
                             new
                             {
                                 c.c_i_id,
                                 c.c_i_type,
                                 c.c_i_subject
                             };
                if (!String.IsNullOrEmpty(type))
                {
                    result = result.Where(c => c.c_i_type == Int32.Parse(type));
                }
                if (!String.IsNullOrEmpty(subject))
                {
                    result = result.Where(c => c.c_i_subject == Int32.Parse(subject));
                }
                return result.Count();
            }
        }

        public List<DefaultDataPack> GetProductDetail(int pid)
        {
            List<DefaultDataPack> list = new List<DefaultDataPack>();
            using (mysql_companyEntities ctx = new mysql_companyEntities())
            {
                var result = (from c in ctx.tb_product
                              where c.c_i_id == pid
                              select new
                              {
                                  c.c_i_id,
                                  c.c_nvc_title,
                                  c.c_nvc_author,
                                  c.c_dt_pbdate,
                                  c.c_i_hit,
                                  c.c_nvc_content,
                                  c.c_nvc_from,
                                  c.c_nvc_img
                              }).FirstOrDefault();
                if (result != null)
                {
                    list.Add(new DefaultDataPack()
                    {
                        ID = result.c_i_id,
                        TITLE = result.c_nvc_title,
                        AUTHOR = result.c_nvc_author,
                        DATE = result.c_dt_pbdate.ToShortDateString(),
                        HIT = result.c_i_hit,
                        CONTENT = result.c_nvc_content,
                        FROM = result.c_nvc_from,
                        URL = result.c_nvc_img
                    });
                }
            }
            return list;
        }

        public bool ProductEdit(string copname, ProductDataPack datapack)
        {
            using (mysql_companyEntities ctx = new mysql_companyEntities())
            {
                var item = ctx.tb_product.FirstOrDefault(c => c.c_vc_cpname == copname && c.c_i_id == datapack.ID);
                item.c_nvc_author = datapack.Author;
                item.c_dt_pbdate = DateTime.Parse(datapack.Date);
                item.c_nvc_content = datapack.Content;
                item.c_nvc_from = datapack.From;
                item.c_i_hit = Int32.Parse(datapack.Hit);
                item.c_nvc_title = datapack.Title;
                if (!String.IsNullOrEmpty(datapack.URL))
                {
                    item.c_nvc_img = datapack.URL;
                }     
                return ctx.SaveChanges() == 1;
            }
        }

        public bool ProductDelete(string copname, int pid)
        {
            using (mysql_companyEntities ctx = new mysql_companyEntities())
            {
                var item = ctx.tb_product.FirstOrDefault(c => c.c_vc_cpname == copname && c.c_i_id == pid);
                if (item != null)
                {
                    item.c_b_isdelete = true;
                    return ctx.SaveChanges() == 1;
                }
                return false;
            }
        }


        public bool ProductAdd(string copname, ProductDataPack productDataPack)
        {
            using (mysql_companyEntities ctx = new mysql_companyEntities())
            {
                ctx.tb_product.AddObject(new tb_product() { 
                    c_b_isdelete = false,
                    c_b_istop=false,
                    c_dt_pbdate=DateTime.Now,
                    c_i_hit=Int32.Parse(productDataPack.Hit),
                    c_nvc_author=productDataPack.Author,
                    c_nvc_content=productDataPack.Content,
                    c_nvc_from=productDataPack.From,
                    c_nvc_title = productDataPack.Title,
                    c_vc_cpname=copname,
                    c_nvc_img = productDataPack.URL
                });
                return ctx.SaveChanges() == 1;
            }
        }
    }
}
